package com.app.backend.common.base;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

/**
 * @program: BaseService
 * @description:
 * @author: Zhun.Xiao
 * @create: 2020-02-08 13:07
 **/
@FunctionalInterface
public interface BaseService<T, ID extends Serializable> {

    BaseDao<T, ID> getRepository();


    default T get(ID id) {
        return getRepository().findById(id).orElse(null);
    }

    default List<T> getAll() {
        return getRepository().findAll();
    }

    default Long getTotalNumber() {
        return getRepository().count();
    }

    default T save(T entity) {
        return getRepository().save(entity);
    }

    default T update(T entity) {
        return getRepository().saveAndFlush(entity);
    }


    default void delete(T entity) {
        getRepository().delete(entity);
    }


    default void deleteById(ID id) {
        getRepository().deleteById(id);
    }


    default void deleteBatch(Iterable<T> entities) {
        getRepository().deleteAll(entities);
    }


    default void flush() {
        getRepository().flush();
    }

    default List<T> findAll(Specification<T> specification) {
        return getRepository().findAll(specification);
    }

    default Page<T> findAll(Pageable pageable) {
        return getRepository().findAll(pageable);
    }


    default Page<T> findAll(Specification<T> specification, Pageable pageable) {
        return getRepository().findAll(specification, pageable);
    }


    default long count(Specification<T> specification) {
        return getRepository().count(specification);
    }


}